#include <iostream>
#include <map>
using namespace std;

signed main()
{
    int n;
    cin>>n;
    map<string,pair<string,string>> ma;
    for(int i=0;i<n;i++){
        string s1,s2,s3;
        cin>>s1>>s2>>s3;
        ma[s1] = {s2,s3};
    }
    string mint = "99:99:99",maxt = "00:00:00";
    string ans1,ans2;
    for(auto it:ma){
        if(it.second.first<mint){
            mint = it.second.first;
            ans1 = it.first;
        }
        if(it.second.second>maxt){
            maxt = it.second.second;
            ans2 = it.first;
        }
    }
    cout<<ans1<<" "<<ans2<<"\n";
    return 0;
}
